package com.dx.mes.service.webservice.service;

import com.dx.common.table.his.His_ret_prd_info;
import com.dx.common.table.ppt.Bis_data;
import com.dx.common.table.ppt.Ret_line_wo;
import com.dx.common.table.ppt.Ret_wo;
import com.dx.common.table.ppt.Trans_prd_sap;
import com.dx.mes.common.bean.erp.EFQCRPT;
import com.dx.mes.common.core.rabbitmq.BaseConnFactory;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.transcoder.util.GUIDGenerator16;
import com.dx.mes.common.transcoder.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.dx.mes.common.bean.def.GenericDef.EVT_MES_SAP_FQC;
import static com.dx.mes.common.bean.def.GenericDef._NG;

/**
 * Created by admin on 2018/4/9.
 */
@Service("msapfqcService")
public class MsapfqcService {

    @Autowired
    private IHisRetPrdInfoDao hisRetPrdInfoDao;
    @Autowired
    private ITransPrdSapDao transPrdSapDao;
    @Autowired
    private IRetLineWoDao  retLineWoDao;
    @Autowired
    private IBisDataDao bisDataDao;
    @Autowired
    private IRetWODao retWODao;
    @Resource
    private BaseConnFactory baseConnFactory;

    public void subMainProc() {

        //获得FQC站点上一次报工时间
        String sqlM = "select * from trans_prd_sap where ope_id_fk = 'J0800' and evt_cate='MSFQ'";
        List<Trans_prd_sap> transPrdSapList = transPrdSapDao.findBySQL(sqlM,Trans_prd_sap.class);

        List<His_ret_prd_info> his_ret_prd_infos =null;
        if(transPrdSapList!=null && transPrdSapList.size()>0){
            Trans_prd_sap trans_prd_sap = transPrdSapList.get(0);
            Format dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date  = dateFormat.format(trans_prd_sap.getMes_update_time());

            String hql ="FROM His_ret_prd_info where cr_ope_id_fk='J0800' and evt_cate='LGON'" +
                    " and evt_timestamp > to_date('" +date+"','yyyy-mm-dd hh24:mi:ss')";
            his_ret_prd_infos = hisRetPrdInfoDao.find(hql);
        }else {//该FQC第一次报工
            String hql ="FROM His_ret_prd_info where cr_ope_id_fk='J0800' and evt_cate='LGON'";
            his_ret_prd_infos = hisRetPrdInfoDao.find(hql);
        }
        //整理数据
        Map<String,List<His_ret_prd_info>> map = new HashMap<>();
        for(His_ret_prd_info his_ret_prd_info : his_ret_prd_infos){
           if(map.containsKey(his_ret_prd_info.getWo_id_fk())){
               map.get(his_ret_prd_info.getWo_id_fk()).add(his_ret_prd_info);
           }else{
               List<His_ret_prd_info> hisList = new ArrayList<>();
               hisList.add(his_ret_prd_info);
               map.put(his_ret_prd_info.getWo_id_fk(),hisList);
           }
        }
        List<EFQCRPT> efqcrptList = new ArrayList<>();
        Timestamp current_timestamp = Util.getCurrentTimestamp();
        for(Map.Entry<String, List<His_ret_prd_info>> entry : map.entrySet()){


            Format dateFormat = new SimpleDateFormat("yyyyMMdd");
            String date = dateFormat.format(new Date());
            Format dateFormat1 = new SimpleDateFormat("HHmmss");
            String time = dateFormat1.format(new Date());


            //entry.getValue().size()是批次数量
            Ret_wo ret_wo = retWODao.get(entry.getKey());
            String hqlX ="from Ret_line_wo where wo_id_fk='" +entry.getKey()+"' and  ope_id_fk='J0800' ";
            List<Ret_line_wo>  lineWos = retLineWoDao.find(hqlX);

            //prdSeqIdList 是抽检数量
            String sql1 ="select distinct prd_seq_id from His_ret_prd_info where cr_ope_id_fk='J0800' and wo_id_fk='" +entry.getKey()+"'" +
                    " and (evt_cate='FQCD' or evt_cate='FQCW')  ";
            List<String> prdSeqIdList  = hisRetPrdInfoDao.findBySQL(sql1);

            //NgList 是不合格数量
            String sqlNG ="select distinct prd_seq_id from His_ret_prd_info where cr_ope_id_fk='J0800' and wo_id_fk='" +entry.getKey()+"'" +
                    " and (ext_4='NG' or ext_6='NG')  ";
            List<String> NgList  = hisRetPrdInfoDao.findBySQL(sqlNG);

            //DXList.size 是电讯检数量  WGList外观检数量
            String hql2 ="FROM His_ret_prd_info where cr_ope_id_fk='J0800' and wo_id_fk='"+entry.getKey()+"' and evt_cate='FQCD'";
            List<His_ret_prd_info> DXList = hisRetPrdInfoDao.find(hql2);
            for(His_ret_prd_info his_ret_prd_info : DXList){
                if(_NG.equals(his_ret_prd_info.getExt_3())){
                    String hqlA ="from Bis_data where data_cate='DFCT' and data_id='"+his_ret_prd_info.getDef_code()+"'";
                    List<Bis_data>  bisData = bisDataDao.find(hqlA);
                    EFQCRPT efqcrpt = new EFQCRPT();
                    efqcrpt.setWERKS(ret_wo.getFab_id_fk());
                    efqcrpt.setAUFNR(ret_wo.getWo_id());
                    efqcrpt.setZZD("J0800");
                    efqcrpt.setZXT(lineWos.get(0).getLine_id_fk());
                    efqcrpt.setMANTNR(ret_wo.getMdl_id_fk());
                    efqcrpt.setZMESLH(his_ret_prd_info.getPrd_seq_id());
                    efqcrpt.setZPCSL(String.valueOf(entry.getValue().size()));
                    efqcrpt.setZJYLX("电讯检");
                    efqcrpt.setZJYJG("NG");
                    efqcrpt.setZQXDM(his_ret_prd_info.getDef_code());
                    efqcrpt.setZQXYY(bisData.get(0).getData_desc());
                    efqcrpt.setZJCRY(his_ret_prd_info.getExt_3());
                    efqcrpt.setZCYSL(String.valueOf(prdSeqIdList.size()));
                    efqcrpt.setZBHGSL(String.valueOf(NgList.size()));
                    efqcrpt.setMEINS("PCS");
                    efqcrpt.setZJYRQ(date);
                    efqcrpt.setZJYSJ(time);
                    efqcrptList.add(efqcrpt);
                }
            }
            String hql3 ="FROM His_ret_prd_info where cr_ope_id_fk='J0800' and wo_id_fk='"+entry.getKey()+"' and evt_cate='FQCW'";
            List<His_ret_prd_info> WGList = hisRetPrdInfoDao.find(hql3);
            for(His_ret_prd_info his_ret_prd_info : WGList){
                if(_NG.equals(his_ret_prd_info.getExt_6())){
                    String hqlB ="from Bis_data where data_cate='DFCT' and data_id='"+his_ret_prd_info.getDef_code()+"'";
                    List<Bis_data>  bisData = bisDataDao.find(hqlB);
                    EFQCRPT efqcrpt = new EFQCRPT();
                    efqcrpt.setWERKS(ret_wo.getFab_id_fk());
                    efqcrpt.setAUFNR(ret_wo.getWo_id());
                    efqcrpt.setZZD("J0800");
                    efqcrpt.setZXT(lineWos.get(0).getLine_id_fk());
                    efqcrpt.setMANTNR(ret_wo.getMdl_id_fk());
                    efqcrpt.setZMESLH(his_ret_prd_info.getPrd_seq_id());
                    efqcrpt.setZPCSL(String.valueOf(entry.getValue().size()));
                    efqcrpt.setZJYSJ("外观检");
                    efqcrpt.setZJYJG("NG");
                    efqcrpt.setZQXDM(his_ret_prd_info.getDef_code());
                    efqcrpt.setZQXYY(bisData.get(0).getData_desc());
                    efqcrpt.setZJCRY(his_ret_prd_info.getExt_5());
                    efqcrpt.setZCYSL(String.valueOf(prdSeqIdList.size()));
                    efqcrpt.setZBHGSL(String.valueOf(NgList.size()));
                    efqcrpt.setMEINS("PCS");
                    efqcrpt.setZJYRQ(date);
                    efqcrpt.setZJYSJ(time);
                    efqcrptList.add(efqcrpt);
                }
            }
        }
        Trans_prd_sap trans_prd_sap = null;
        String sqlY = "select * from trans_prd_sap where ope_id_fk = 'J0800' and evt_cate='MSFQ'";
        List<Trans_prd_sap> opelist = transPrdSapDao.findBySQL(sqlY,Trans_prd_sap.class);
        if(opelist!=null && opelist.size()>0){
            trans_prd_sap = opelist.get(0);
            trans_prd_sap.setOpe_id_fk("J0800");
            trans_prd_sap.setMes_update_time(current_timestamp);
            trans_prd_sap.setPrd_qty(0);
            trans_prd_sap.setSap_id("");
            trans_prd_sap.setEvt_usr("MES");
            trans_prd_sap.setEvt_cate(EVT_MES_SAP_FQC);//报工操作
            trans_prd_sap.setEvt_timestamp(current_timestamp);;
        }else {
            trans_prd_sap  =  new Trans_prd_sap();
            trans_prd_sap.setEvt_seq_id(GUIDGenerator16.javaGUID());
            trans_prd_sap.setOpe_id_fk("J0800");
            trans_prd_sap.setSap_id("");
            trans_prd_sap.setPrd_qty(0);
            trans_prd_sap.setMes_update_time(current_timestamp);
            trans_prd_sap.setEvt_usr("MES");
            trans_prd_sap.setEvt_cate(EVT_MES_SAP_FQC);//报工操作
            trans_prd_sap.setEvt_timestamp(current_timestamp);
        }
        transPrdSapDao.saveOrUpdate(trans_prd_sap);
        try {
            if(efqcrptList.size()>0){
//                baseConnFactory.addSAPREQProc("EFQCRPT", JacksonUtil.toJSONStr(efqcrptList));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
